//
//  MainQutoeCell.m
//  ZITHER
//
//  Created by weixin on 2018/8/13.
//  Copyright © 2018年 weixn. All rights reserved.
//



#import "MainQutoeCell.h"
#import "SKUDetailReceiveVC.h"

@implementation MainQutoeCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
//    self.backgroundColor = [UIColor randomColor];
    
    [self.BackView setCornerRadiusWithRadius:15 isMasks:YES];
    
    [self.BackView setBorderWithColor:[UIColor RGBColorWithR:224 g:224 b:224] width:1];
    
    [self.Userimage setCornerRadiusWithRadius:7 isMasks:YES];

    [self.imageV setCornerRadiusWithRadius:7 isMasks:YES];

    [UIColor setDefaulGradiennBackgroundColors:self.enquiryLab];
    
}


-(void)setModelType1:(MyEnquiryListModel *)modelType1{
    _modelType1 = modelType1;

    
}

-(void)setModelType2:(MyEnquiryListDataModel *)modelType2{
    _modelType2 = modelType2;
    //  判断是否展示商家用户数据
        if (modelType2.bizInfoList.count !=0) {
            self.V3Height.constant = 50;
            NSString *str = [NSString stringWithFormat:@"%@%@",RootUrlImage,modelType2.bizInfoList[0].bizPortrait];

            [self.Userimage sd_setImageWithURL:[NSURL URLWithString:str]];
            self.UserName.text =modelType2.bizInfoList[0].bizNickName;
           
            self.UserInfo.text = [NSString stringWithFormat:@"及%d个已接受报价的商家", [modelType2.acceptPriceCount intValue] -1];
            self.BackView3.hidden = NO;
            
        }else{
            self.V3Height.constant = 0;
            self.BackView3.hidden = YES;
        }
 
    self.takeinLab.text = modelType2.acceptPriceCount;
    self.receiveLab.text = modelType2.priceCount;
    
    NSString *strImage = [NSString stringWithFormat:@"%@%@",RootUrlImage,modelType2.imageDefault];
    
    [self.imageV sd_setImageWithURL:[NSURL URLWithString:strImage]];
    
    self.modelLab.text = modelType2.brandNameNative;
    self.brandLab.text = modelType2.goodName;

    CGFloat H1 = 0;
    CGFloat h2 = 0;
    CGFloat h3 = 0;

    CGFloat q1 = 0;
    CGFloat p1 = 0;
    CGFloat e1 = 0;

 
    
    if (![modelType2.freshPriceCount isEqualToString:@"0"]) {
        [self.enquiryLab setTitle:[NSString stringWithFormat:@"  新收到%@个报价  ",modelType2.freshPriceCount] forState:UIControlStateNormal];
        H1 = 30;
        q1 = 10;
    }
    
    if (![modelType2.openSourceCount isEqualToString:@"0"]) {
        [self.publicityLab setTitle:[NSString stringWithFormat:@"  %@个新货源已公开  ",modelType2.openSourceCount] forState:UIControlStateNormal];

        h2 = 30;
        p1 = 10;
    }
    
 
    NSString *str =  [self pleaseInsertStarTimeo:modelType2.userEnquiryInfo.refreshTime andInsertEndTime:[self getCurrentTimes]];
    if (![str isEqualToString:@"0"]) {
       str = [NSString stringWithFormat:@"  你已经%@天没刷新询价了",str];

         [self.quoteLab setTitle:str forState:UIControlStateNormal];
        h3 = 30;
        e1 = 10;
    }
    
    
    self.H1.constant = H1;
    self.h2.constant = h2;
    self.h3.constant = h3;
    self.q1.constant = q1;
    self.p1.constant = p1;
    self.e1.constant = e1;
    
    
}

- (NSString *)pleaseInsertStarTimeo:(NSString *)time1 andInsertEndTime:(NSString *)time2{
    // 1.将时间转换为date
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    NSDate *date1 = [formatter dateFromString:time1];
    NSDate *date2 = [formatter dateFromString:time2];
    // 2.创建日历
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSCalendarUnit type = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
    // 3.利用日历对象比较两个时间的差值
    NSDateComponents *cmps = [calendar components:type fromDate:date1 toDate:date2 options:0];
    // 4.输出结果
    if (cmps.day == 0 ) {
        self.quoteLab.enabled = NO;
    }else{
        self.quoteLab.enabled = YES;
    }
    NSLog(@"%@",[NSString stringWithFormat:@"%ld",cmps.day]);
    return [NSString stringWithFormat:@"%ld",cmps.day];
}

-(NSString*)getCurrentTimes{
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    
    // ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
    
    [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
    
    //现在时间,你可以输出来看下是什么格式
    
    NSDate *datenow = [NSDate date];
    
    //----------将nsdate按formatter格式转成nsstring
    
    NSString *currentTimeString = [formatter stringFromDate:datenow];
    
    NSLog(@"currentTimeString =  %@",currentTimeString);
    
    return currentTimeString;
    
}


- (IBAction)EnquiryLab:(id)sender {
    NSLog(@"新收到报价");
    
    SKUDetailReceiveVC *vc = [[SKUDetailReceiveVC alloc]init];
    vc.indexType = @"1";
    vc.str = @"提醒商家报价";
    vc.leb.text = @"新收到的报价";
    vc.goodsidNew = self.modelType2.userEnquiryInfo.goodsId;
    [self.getNav pushViewController:vc animated:YES];
    
}
- (IBAction)PublicityLab:(id)sender {
    
    SKUDetailReceiveVC *vc = [[SKUDetailReceiveVC alloc]init];
    vc.userEnquiryId = self.modelType2.userEnquiryInfo.userEnquiryId;
    vc.goodsid = self.modelType2.userEnquiryInfo.goodsId;
    vc.str = @"公开新的货源";
    [self.getNav pushViewController:vc animated:YES];

}

- (IBAction)QuoteLab:(id)sender {
    NSLog(@"你已经五天没刷新询价了");
    
    [[serviceDefualt loadrefreshEnquiry:self.modelType2.userEnquiryInfo.goodsId reponseObjctClass:nil]subscribeNext:^(ResponseData * x) {
        if (x.success) {
            self.quoteLab.enabled = NO;
            [self.quoteLab setTitle:@"  你已经0天没刷新询价了  " forState:UIControlStateNormal];

            [Tools showTips:@"刷新成功"];
            
        }else{
             [Tools showTips:x.errorInfo];
        }
    }];
}







@end
